import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/common/count_down_timer.dart';
import 'package:cbec_app/components/decoration/decoration_goods.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'decoration_cut_down_activity_ctr.dart';

//店铺装修-秒杀、拼团、特价组件
class DecorationKillSale extends StatefulWidget {
  ///秒杀配置
  final Map<String, dynamic> info;

  ///商品id key值
  final String idKey;

  ///商品现价 key值
  final String curPriceKey;

  ///商品原价 key值
  final String originPriceKey;

  ///商品类型 0-普通商品 1-特价商品 2-拼团商品 3-秒杀商品 4-砍价商品 5-预售商品
  final int goodsType;

  const DecorationKillSale({
    super.key,
    required this.info,
    this.idKey = 'id',
    this.curPriceKey = 'price',
    this.goodsType = 0,
    this.originPriceKey = 'priceMarket',
  });

  @override
  State<DecorationKillSale> createState() => _DecorationKillSaleState();
}

class _DecorationKillSaleState extends State<DecorationKillSale> {
  late DecorationCutDownActivityCtr controller;

  @override
  void initState() {
    super.initState();
    String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
    controller = Get.put(
      DecorationCutDownActivityCtr(),
      tag: timestamp, // 使用 tag 以区分不同的实例
    );
  }

  @override
  Widget build(BuildContext context) {
    controller.getActivityComponentInfo(widget.info);
    return Obx(() {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          header(),
          DecorationGoods(
            info: widget.info,
            goodsList: controller.activityGoodsList,
            idKey: widget.idKey,
            curPriceKey: widget.curPriceKey,
            originPriceKey: widget.originPriceKey,
            goodsType: widget.goodsType,
          )
        ],
      );
    });
  }

  String headerTitle() {
    return controller.activityEntity.value.status == 2 ? "距结束" : "距开始";
  }

  int headerTitleTime() {
    return (controller.activityEntity.value.status == 2 ? controller.activityEntity.value.endTime ?? 0 : controller.activityEntity.value.startTime ?? 0) * 1000;
  }

  ///头部配置
  Widget header() {
    return Container(
      padding: const EdgeInsets.all(10),
      color: widget.info['top_bg_color'] == null ? Colors.white : Tools.fromHex(widget.info['top_bg_color']),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              widget.info['show_count_down'] == '1' && controller.timeStamp > 0
                  ? Row(
                      children: [
                        Text(widget.info['activity_name'] ?? '', style: setTextStyle(16, FontWeight.w700, Tools.fromHex(widget.info['activity_name_color']))),
                        const SizedBox(width: 10),
                        Text(headerTitle(), style: setTextStyle(14, FontWeight.w400, Tools.fromHex(widget.info['activity_name_color']))),
                        const SizedBox(width: 6),
                        CountDownTimer(
                          timeStamp: headerTitleTime(),
                          countDownStyle: CountDownStyle.box,
                          boxBackgroundColor: Tools.fromHex(widget.info['activity_name_color']),
                          colonColor: Tools.fromHex(widget.info['activity_name_color']),
                          style: setTextStyle(12, FontWeight.w300, Colors.white),
                          callback: () => {},
                        )
                      ],
                    )
                  : emptyWidget(),
            ],
          ),
          widget.info['more_show'] == '1' && widget.info['show_count_down'] == '1' && controller.timeStamp > 0 ? lookMore() : emptyWidget()
        ],
      ),
    );
  }

  ///查看更多样式
  Widget lookMore() {
    switch (widget.info['more_btn_style']) {
      case 2: //文字+箭头
        return BaseInteractive(
          childWidget: Row(
            children: [
              Text(widget.info['more_btn_text'], style: setTextStyle(14, FontWeight.w400, Tools.fromHex(widget.info['more_btn_color']))),
              Icon(
                Icons.navigate_next,
                color: Tools.fromHex(widget.info['more_btn_color']),
              )
            ],
          ),
          onClick: () {
            jumpPage();
          },
        );
      case 3: //箭头
        return BaseInteractive(
          childWidget: Icon(
            Icons.navigate_next,
            color: Tools.fromHex(widget.info['more_btn_color']),
          ),
          onClick: () {
            jumpPage();
          },
        );
      case 4: //圆角文字按钮
        return BaseInteractive(
          childWidget: Container(
            padding: EdgeInsets.all(5),
            decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(20)), border: Border.all(color: Tools.fromHex(widget.info['more_btn_color']))),
            child: Text(widget.info['more_btn_text'], style: setTextStyle(14, FontWeight.w400, Tools.fromHex(widget.info['more_btn_color']))),
          ),
          onClick: () {
            jumpPage();
          },
        );
      case 1: //文字
      default:
        return BaseInteractive(
          onClick: () {
            jumpPage();
          },
          childWidget: Text(widget.info['more_btn_text'], style: setTextStyle(14, FontWeight.w400, Tools.fromHex(widget.info['more_btn_color']))),
        );
    }
  }

  ///跳转页面
  void jumpPage() {
    switch (widget.goodsType) {
      case 1: //1-特价商品
        Get.toNamed(Routes.discountGoodsList);
        break;
      case 2: //2-拼团商品
        Get.toNamed(Routes.groupBuyingPage);
        break;
      case 3: //3-秒杀商品
        Get.toNamed(Routes.killSalesPage);
        break;
      case 4: //4-砍价商品
        Get.toNamed(Routes.bargainingPage);
        break;
      case 5: //5-预售商品
        Get.toNamed(Routes.preSales);
        break;
    }
  }

  ///获取页面边距
  double getMargin() {
    return double.parse(widget.info['page_margin'].toString());
  }

  ///获取图片间距
  double getImgMargin() {
    return double.parse(widget.info['goods_margin'].toString());
  }
}
